package com.byq.mm.smartlib.application;

import java.util.Map;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.Build;
import android.preference.PreferenceManager;

import com.byq.mm.abstractx.cluster.server.executor.Executor;
import com.byq.mm.abstractx.device.Cluster;
import com.byq.mm.abstractx.device.Device;
import com.byq.mm.smartlib.SmartActionConstants;
import com.byq.mm.smartlib.component.SmartPlayer;
import com.byq.mm.smartlib.connection.ConnectivityReceiver;
import com.byq.mm.smartlib.connection.LoginManager;
import com.byq.mm.smartlib.connection.LoginReceiver;
import com.byq.mm.smartlib.engine.AndroidSmartEngine;
import com.byq.mm.smartlib.engine.NotificationHandler;


abstract public class SmartApplication extends Application{
	protected AndroidSmartEngine androidSmartEngine;
	private BroadcastReceiver loginReceiver;
	BroadcastReceiver connectivityReceiver;

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();

		androidSmartEngine = new AndroidSmartEngine(this,getNotificationHandler());
		androidSmartEngine.getCoreEngine().getServerDeviceManager().reset(getLocalServerDevice());
		initExecutor();
        try {
            registerAllReceiver();
        } catch (IntentFilter.MalformedMimeTypeException e) {
            throw new RuntimeException(e);
        }
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

		SharedPreferences.OnSharedPreferenceChangeListener listener =
				new SharedPreferences.OnSharedPreferenceChangeListener() {
					public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
						if (key.equals("username")) {
							String newUsername = sharedPreferences.getString(key, "Default");
							// Update UI or perform necessary actions
						}
					}
				};

		sharedPreferences.registerOnSharedPreferenceChangeListener(listener);

    }
	private void registerAllReceiver() throws IntentFilter.MalformedMimeTypeException {
		
		loginReceiver = new LoginReceiver();

		IntentFilter filter = new IntentFilter(SmartActionConstants.RELOGIN);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
			registerReceiver(loginReceiver, filter, RECEIVER_NOT_EXPORTED);
		}
		
		connectivityReceiver = new ConnectivityReceiver();
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
			registerReceiver(connectivityReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION), RECEIVER_NOT_EXPORTED);
		}
	}
	@Override
	public void onTerminate() {
		super.onTerminate();
		this.unregisterReceiver(loginReceiver);
		this.unregisterReceiver(connectivityReceiver);
	}
	public AndroidSmartEngine getAndroidSmartEngine() {
		return androidSmartEngine;
	}
	
	void initExecutor(){
		Device root = androidSmartEngine.getCoreEngine().getServerDeviceManager().getLocalServerDevice();
		String deviceId = root.getId();
		for(Cluster cluster : root.getSupportedClusters()){
			
			Executor e = getExecutor(root.getId(), cluster.getId());
			
				androidSmartEngine.getCoreEngine().getExecutorManager().register(deviceId, cluster.getId(), e);

		}
	}
	protected abstract Executor getExecutor(String deviceId, String clusterId);
	public LoginManager getLoginManager(){
		return androidSmartEngine.getLoginManager();
	}
	
	abstract public Device getLocalServerDevice();
	abstract public NotificationHandler getNotificationHandler();
	abstract public SmartPlayer getSound();
	abstract public Map<String, Executor> getRootLocalExecutors();
}
